﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UniRlv.COL;
using UniRlv.Utility;

namespace UniRlv.UI.Main.Widget
{
    public partial class FrmExpDeal : Form
    {
        BindingList<CodeInfo> CodeInfos;
        public string Tip { get; set; }
        public List<string> Codes { get; set; }

        public bool ShowCodeState { get; set; }

        /// <summary>
        /// 是否带出相邻条码
        /// </summary>
        public bool Ajacent { get; set; }

        public bool IfCheckDuplicate { get; set; }

        public FrmExpDeal()
        {
            InitializeComponent();
            IfCheckDuplicate = true;
        }

        private void FrmExpDeal_Load(object sender, EventArgs e)
        {
            lbTip.Text = Tip;

            if (Codes!=null)
            {
                foreach (var item in Codes)
                {
                    AddCode(item);
                }
            }

            CodeInfos = new BindingList<CodeInfo>();
            UIHelper.InitDataGridView(dgvDetails);
            dgvDetails.DataSource = CodeInfos;

            chkAjacent.Checked = Ajacent;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;

            if (Codes == null)
            {
                Codes = new List<string>();
            }

            Codes.Clear();
            foreach (DataGridViewRow row in dgvDetails.Rows)
            {
                if (Convert.ToBoolean(row.Cells[0].EditedFormattedValue))
                {
                    CodeInfo ci = row.DataBoundItem as CodeInfo;
                    Codes.Add(ci.code);
                }
            }

            Close();
        }

        private void FrmExpDeal_KeyDown(object sender, KeyEventArgs e)
        {
            if (txtCode.Focus() && e.KeyCode == Keys.Enter)
            {
                AddCode(txtCode.Text.Trim());

                if (!Ajacent)
                {
                    txtCode.Text = "";
                    txtCode.Focus();
                }
                
            }
        }

        public void AddCode(string code)
        {
            string bit24 = AppHelper.SYZSGet24BitFromCodeNoCheck(code);
            if (string.IsNullOrEmpty(bit24))
            {
                MessageBox.Show("条码内容非法：" + code);
                return;
            }

            CodeInfo ci = CodePrcUtility.FindCode(bit24);

            if (Ajacent)
            {
                if (ci == null)
                {
                    MessageBox.Show("条码未入库，无相邻条码");
                    return;
                }

                CodeInfos.Clear();
                int idx = CodePrcUtility.FindCodePos(ci);
                List<CodeInfo> tmpCodes = CodePrcUtility.FindArrayByPos1(ci.stationNumber, idx - (int)numAjacentQty.Value/2, (int)numAjacentQty.Value);

                foreach (var item in tmpCodes)
                {
                    CodeInfos.Add(item);
                    dgvDetails.Rows[dgvDetails.Rows.Count - 1].Cells[0].Value = true;
                }

                    
                txtCode.Text = code;
                txtCode.SelectAll();
            }
            else
            {
                if (ci == null)
                {
                    ci = new CodeInfo();
                    ci.code = bit24;
                    ci.remarks = "码不存在";
                }

                if (CodeInfos.Contains(ci) && IfCheckDuplicate)
                {
                    MessageBox.Show("不能重复扫描！");
                    return;
                }

                CodeInfos.Add(ci);

                dgvDetails.Rows[dgvDetails.Rows.Count - 1].Cells[0].Value = true;

                string msg = string.Empty;

                if (ShowCodeState)
                {
                    CodePrcUtility.QueryCodeState(ci.code, ref msg);
                    MessageBox.Show(msg, "提示");
                }
            }

        }

        private void chkAjacent_CheckedChanged(object sender, EventArgs e)
        {
            numAjacentQty.Enabled = chkAjacent.Checked;
            Ajacent = numAjacentQty.Enabled;
        }
    }
}
